package com.boyuan.huijiaoyu;

import java.util.ArrayList;
import java.util.List;

import com.boyuan.aboutcase.AddCaseMembersActivity;
import com.boyuan.aboutcase.CaseDetailActivity;
import com.boyuan.aboutcase.NewCaseDetailActivity;
import com.example.contactfragment.ContactGroupService;
import com.example.contactfragment.ContactService;
import com.example.contactfragment.GroupMember;
import com.example.contactfragment.HomeContactMyCaseGroupActivity;
import com.example.contactfragment.HomeContactMyGroupActivity;
import com.example.contactfragment.LognUser;
import com.example.huanxin.GroupDetailActivity;
import com.example.newfragment.DetailPerson;
import com.example.newfragment.HomeService;
import com.example.newfragment.members;
import com.magicsoft.app.entity.CaseDetailEntity;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.app.helper.ToastHelper;
import com.magicsoft.app.wcf.CaseService;
import com.magicsoft.app.wcf.listener.GetMultilParamsListener;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;
import com.magicsoft.app.wcf.listener.PostRecordResponseListener;
import com.magicsoft.constant.Constant;
import com.magicsoft.weitown.fragment.HomeNewFragment;
import com.magicsoft.weitown.ui.CustomDialog;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class CaseMemberActivity extends Activity implements OnClickListener{

	private String yonghuID;
	ArrayList<GroupMember> groups = new ArrayList<GroupMember>();
	ArrayList<String> phonelist = new ArrayList<String>();
	ArrayList<String> usernamelist = new ArrayList<String>();
	private TextView tv_title;
	int nun;
	boolean isreqok = false;
	private GridView gridview;
	private MyAdapter adapter;
	boolean isdel = false;
	boolean isgetidOK = false;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;
	private CaseService caseService;
	private CaseDetailEntity caseDetailEntity;
	private String realname;
	private List<members> memberlist;
	private String case_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_case_member);
		SharedPreferences sp = getSharedPreferences("a.xml", 0);
		yonghuID = sp.getString("id", "");
		realname = sp.getString("realname", "");
		Intent intent = getIntent();
		caseDetailEntity = (CaseDetailEntity) intent.getSerializableExtra("caseDetailEntity");
		case_id = caseDetailEntity.getCid();
		memberlist = caseDetailEntity.getMembers();
		creater_id = caseDetailEntity.getCreater_id();
		tv_title = (TextView) findViewById(R.id.group_name123);
		tv_title.setText("case成员("+memberlist.size()+")");
		initUI();
	}
	
	private void initUI() {
		gridview = (GridView) findViewById(R.id.gridView1);
		adapter = new MyAdapter();
		gridview.setAdapter(adapter);
		gridview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
					long arg3) {
				if(arg2 == memberlist.size()){
					Intent intent = new Intent(CaseMemberActivity.this, AddCaseMembersActivity.class);
					intent.putExtra("caseDetailEntity", caseDetailEntity);
					startActivityForResult(intent , 001);
				}
			}
		});
		
	}
	private HomeService homeService;
	private String creater_id;
//	private void getCaseDetailInfo() {
//		if (homeService == null) {
//			homeService = new HomeService(this);
//		}
//		SharedPreferences sp = getSharedPreferences("a.xml", 0);
//		String corp_id = sp.getString("username", "");
//		homeService.GetCaseDetail(case_id, corp_id,
//				new GetMultilParamsListener() {
//
//					@Override
//					public void onFinish(Object... params) {
//						caseDetailEntity = (CaseDetailEntity) params[0];
//						memberlist = caseDetailEntity.getMembers();
//						adapter.notifyDataSetChanged();
//						tv_title.setText("case成员("+memberlist.size()+")");
////						// 发送广播更新动态
////						Intent intent = new Intent();
////						intent.setAction(Constant.GETDYNAMIC);
////						intent.putExtra("caseDetailEntity", caseDetailEntity);
////						sendBroadcast(intent);
////						//发送广播上传更新
////						Intent intent2 = new Intent();
////						intent2.setAction(Constant.GETATTACH);
////						intent2.putExtra("caseDetailEntity", caseDetailEntity);
////						sendBroadcast(intent2);
//					}
//
//					@Override
//					public void onFailed(String Message) {
//						ToastHelper.showMsg(CaseMemberActivity.this, Message,
//								false);
//					}
//				});
//	}
	
	private void getCaseDetailInfo2() {
	if (homeService == null) {
		homeService = new HomeService(this);
	}
	homeService.getCaseAndIm(case_id,
			new GetMultilParamsListener() {

				@Override
				public void onFinish(Object... params) {
					caseDetailEntity = (CaseDetailEntity) params[0];
					memberlist = caseDetailEntity.getMembers();
					adapter.notifyDataSetChanged();
					tv_title.setText("case成员("+memberlist.size()+")");
				}

				@Override
				public void onFailed(String Message) {
					ToastHelper.showMsg(CaseMemberActivity.this, Message,
							false);
				}
			});
}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(data != null){
			if(requestCode == 001 && resultCode == 222){
//				getCaseDetailInfo();
				getCaseDetailInfo2();
				adapter.notifyDataSetChanged();
				
			}
		}
	}
	
	class MyAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			return memberlist.size() + 1;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			View inflate = getLayoutInflater().inflate(R.layout.grid_member, null);
			final TextView tv = (TextView) inflate.findViewById(R.id.textView1);
			ImageView imag_tou = (ImageView) inflate.findViewById(R.id.imag_tou);
			imageLoader = ImageLoader.getInstance();
			options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.default_header)
			.showImageForEmptyUri(R.drawable.default_header)
			.showImageOnFail(R.drawable.default_header).cacheInMemory(true)
			.cacheOnDisc(true).considerExifParams(true)
			.build();
			if(arg0<=memberlist.size()-1){
				String uri = Constant.photourl + memberlist.get(arg0).getId();
				imageLoader.displayImage(uri, imag_tou, options);
				tv.setText(memberlist.get(arg0).getName());
			}else if(arg0 == memberlist.size()){
				imag_tou.setImageResource(R.drawable.tianjia);
				tv.setText("");
			}
			return inflate;
		}
		
	}
	
	/**
	* 点击取消关注按钮
	*/
	public void cancleCase(View view) {
		exitCase();
	}
	
	private void exitCase() {
		final CustomDialog dialog = new CustomDialog(this,
				R.style.qr_dialog);
		dialog.show();
		// 标题
		TextView tv_title = (TextView) dialog.findViewById(R.id.dialog_title);
		tv_title.setText("确定取消关注");
		// 内容
		TextView tv_content = (TextView) dialog
				.findViewById(R.id.dialog_content);
		tv_content.setVisibility(View.INVISIBLE);
		tv_content.setText("");
		// 右上角取消
		ImageView imag_cancel = (ImageView) dialog
				.findViewById(R.id.dialog_img_cancel);
		imag_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
		// 确定按钮
		Button btn_ok = (Button) dialog.findViewById(R.id.dialog_button_ok);
		btn_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
				if(creater_id.equals(yonghuID)){
					Toast.makeText(CaseMemberActivity.this, "创建者不能取消关注！", Toast.LENGTH_SHORT).show();
				}else{
					exitcase();
				}
			}
		});
		// 取消按钮
		Button btn_cancel = (Button) dialog
				.findViewById(R.id.dialog_button_cancel);
		btn_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
	}
		
	public void exitcase(){
		if (caseService == null) {
			caseService = new CaseService(CaseMemberActivity.this);
		}
		caseService.CancelAttention("1", case_id, yonghuID, realname, new PostRecordResponseListener() {
			
			@Override
			public void onFinish(String succString) {
//				Intent intent = new Intent();
//				intent.setAction(Constant.CLOSE_CASEDETAIL);
//				sendBroadcast(intent);
				Intent intent3 = new Intent();
				intent3.setAction(Constant.EXITCASE);
				sendBroadcast(intent3);
				finish();
			}
			
			@Override
			public void onFailed(String failString) {
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.case_member, menu);
		return true;
	}
	
	public void back(View view) {
		setResult(RESULT_OK);
		finish();
	}

	@Override
	public void onClick(View arg0) {
		
	}

}
